#!/bin/sh
#
# $OpenBSD: gdm.rc,v 1.33 2015/07/16 01:51:26 ajacoutot Exp $

daemon="${TRUEPREFIX}/sbin/gdm"

. /etc/rc.d/rc.subr

rc_bg=YES
rc_reload=NO

rc_pre() {
	# XXX OpenBSD needs an implementation of XDG_RUNTIME_DIR
	rm -rf /var/db/gdm/.cache/gnome-shell/runtime-state-*
}

# on boot: wait for ttys to be initialized
if [ -n "${INRC}" ]; then
	unset rc_bg
rc_start() {
	( local i=0
	while ! pgrep -qf "^/usr/libexec/getty "; do
		sleep 1
		[ $((i++)) -ge 10 ] && return 1
	done
	${rcexec} "${daemon} ${daemon_flags} &" ) &
}
fi

rc_stop() {
	pkill -9 -xf "${pexp}"
	pkill -f gdm-session-worker
	pkill -u _x11 -f "^/usr/X11R6/bin/X.*/var/run/gdm"
	# _gdm owned processed may already be gone... or not
	pkill -u _gdm || return 0
}

# /etc/X11/xdm/TakeConsole, fbtab(5)
rc_post() {
	if [ -c /dev/drm0 ]; then
		chown root /dev/drm0
	fi
	# XXX OpenBSD needs an implementation of XDG_RUNTIME_DIR
	rm -rf /var/db/gdm/.cache/gnome-shell/runtime-state-*
}

rc_cmd $1
